package com.blockchain.securewallet.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blockchain.securewallet.R;
import com.blockchain.securewallet.WalletApp;
import com.blockchain.securewallet.base.BaseActivity;
import com.blockchain.securewallet.dialog.IDialogListener;
import com.blockchain.securewallet.dialog.UniversalDialog;
import com.blockchain.securewallet.tools.ActivityLauncher;
import com.blockchain.securewallet.utils.ShowPopUtil;
import com.blockchain.securewallet.utils.StringUtil;
import com.blockchain.securewallet.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class UniversalActivity extends BaseActivity implements IDialogListener {

    @BindView(R.id.tv_title)
    TextView tv_title;

    private ShowPopUtil showPopUtil;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_universal);
        ButterKnife.bind(this);

        tv_title.setText(R.string.slide_universal);
    }


    @OnClick({R.id.tv_account_manager, R.id.tv_setting_see, R.id.tv_setting_secure, R.id.tv_seed_check, R.id.tv_license, R.id.tv_about, R.id.tv_setting, R.id.tv_logout})
    void universalClick(View view) {
        Intent intent = new Intent();

        switch (view.getId()) {
            case R.id.tv_account_manager:
                intent.setClass(this, AccountManagerActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_setting_see:
                //手续费设置
                intent.setClass(this, FeeSettingActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_setting_secure:
                //安全设置
                intent.setClass(this, SecureSettingActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_seed_check:
                //种子查看
                showInputDialog();
                break;
            case R.id.tv_license:
                ToastUtils.showToast("使用许可协议，暂未开放");

                break;
            case R.id.tv_about:
                ToastUtils.showToast("关于，暂未开放");

                break;
            case R.id.tv_setting:
                //现在暂时写调用系统设置在这里
                startActivity(new Intent("android.settings.SETTINGS"));
                break;
            case R.id.tv_logout:
                String title = StringUtil.getResourceString(R.string.account_logout_tips_title);
                String content = StringUtil.getResourceString(R.string.account_logout_tips_content);
                UniversalDialog.showSingeleTipDialog(getSupportFragmentManager(), this, title, content);
                break;
            default:
                break;
        }
    }


    @OnClick(R.id.btn_back)
    void back() {
        finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onItemClick(String... texts) {
        //当前默认显示标题、可返回上一层界面
        ActivityLauncher.gotoWalletInitActivity(this, true);
    }


    void showInputDialog() {
        //首先弹出密码输入框
        if (showPopUtil == null) {
            showPopUtil = new ShowPopUtil(R.layout.password_ensure_dialog, UniversalActivity.this, 0.7, 0.3) {

                @Override
                protected void initView(View view) {

                    final EditText edit_first_password = view.findViewById(R.id.edit_first_password);
                    TextView tv_cancel = view.findViewById(R.id.tv_cancel);
                    TextView tv_ok = view.findViewById(R.id.tv_ok);
                    final TextView tv_tips_pw_err = view.findViewById(R.id.tv_tips_pw_err);

                    edit_first_password.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                            //Logger.ds("beforeTextChanged.");
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                            //如果显示了，就隐藏掉；如果隐藏了，就别管。
                            if (tv_tips_pw_err.getVisibility() == View.VISIBLE) {
                                //Logger.ds("onTextChanged 显示了，隐藏掉.");
                                tv_tips_pw_err.setVisibility(View.GONE);
                            }
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            //Logger.ds("afterTextChanged.");
                        }
                    });

                    tv_cancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            showPopUtil.dismiss();
                            showPopUtil = null;
                        }
                    });

                    tv_ok.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (!edit_first_password.getText().toString().equals(WalletApp.getInstance().getPayPassword())) {

                                if (StringUtil.isEmpty(edit_first_password.getText().toString())) {
                                    tv_tips_pw_err.setText(getString(R.string.tips_password_null));
                                } else {
                                    tv_tips_pw_err.setText(getString(R.string.tips_password_error));
                                }

                                tv_tips_pw_err.setVisibility(View.VISIBLE);

                            } else {
                                //密码正确后，放行
                                ActivityLauncher.gotoWatchSeedActivity(UniversalActivity.this);
                                
                                showPopUtil.dismiss();
                                showPopUtil = null;
                            }

                        }
                    });

                }
            };
        }

        showPopUtil.showPop();
    }

    
}
